﻿using Blm.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using VisionCloud.Core.Model.Enums;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.ModelAttribute;
using static VisionCloud.Core.Model.Ms.MsTask;

namespace VisionCloud.Domain.DtoModel.Ms
{
    /// <summary>
    /// 查询条件对象
    /// </summary>
    public class TaskCm : PageBase
    {
        /// <summary>
        /// 排序字段 ,如果有多个用,分隔
        /// </summary>
        [OrderByField(typeof(TaskLm))]
        public new string OrderBy { get; set; }

        public ProjectStageEnum ProjectStage { get; set; }

        public TaskTypeEnum? EmTaskType { get; set; }

        /// <summary>
        /// 项目名称
        /// </summary>
        public string ProjectName { get; set; }

        /// <summary>
        /// 任务名称
        /// </summary>
        public string TaskName { get; set; }

        /// <summary>
        /// 创建人ID
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// 创建人名字
        /// </summary>
        public string CreateUser { get; set; }

        /// <summary>
        /// 楼栋
        /// </summary>
        public string BuildingBlock { get; set; }

        /// <summary>
        /// 楼单元
        /// </summary>
        public string BuildingFloor { get; set; }

        /// <summary>
        /// 获取数据的时间戳 ,此处以任务的创建时间基准
        /// </summary>
        public DateTime? GetTimestamp { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public TaskStatus? Status { get; set; }

        /// <summary>
        /// 项目Id
        /// </summary>
        public Guid ProjectId { get; set; }

        /// <summary>
        /// 户型图Id
        /// </summary>
        public Guid HouseMapId { get; set; }

        /// <summary>
        /// 最近更新时间
        /// </summary>
        public DateTime? LastUpdatedDate { get; internal set; }

        /// <summary>
        /// 任务类型
        /// </summary>
        public TaskTypeEnum? TaskType { get; set; }

        public Guid TaskId { get; set; }

        /// <summary>
        /// 搜索起始日期
        /// </summary>
        public DateTime? StartDate { get; set; }

        /// <summary>
        /// 搜索截至日期
        /// </summary>
        public DateTime? EndDate { get; set; }

        /// <summary>
        /// 测量任务类型
        /// </summary>
        public MeasureTypeEnum? MeasureType { set; get; }

        /// <summary>
        /// 是否是获取复制任务列表
        /// </summary>
        public bool IsGetCopyList { get; set; } = false;
    }

    /// <summary>
    /// 列表展示对象
    /// </summary>
    public class TaskLm
    {
        public Guid TaskId { get; set; }

        /// <summary>
        /// 项目名称
        /// </summary>
        public string ProjectName { get; set; }

        /// <summary>
        /// 项目ID
        /// </summary>
        public Guid ProjectId { get; set; }

        /// <summary>
        /// 户型图ID
        /// </summary>
        public Guid HouseMapId { get; set; }

        /// <summary>
        /// 楼栋
        /// </summary>
        public string BuildingBlock { get; set; }

        /// <summary>
        /// 楼层
        /// </summary>
        public string BuildingFloor { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string CreateUserName { get; set; }

        /// <summary>
        /// 操作人
        /// </summary>
        public string OperatorName { get; set; }

        /// <summary>
        /// 操作人ID
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateDate { get; set; }

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime LastUpdatedDate { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public string StatusStr { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public MsTask.TaskStatus Status { get; set; }

        /// <summary>
        /// 户型图点
        /// </summary>
        public string HouseMapPoints { get; set; }

        /// <summary>
        /// 工艺
        /// </summary>
        public string Craft { get; internal set; }

        ///// <summary>
        ///// 交付标准
        ///// </summary>
        //public string SupportStandard { get; internal set; }

        /// <summary>
        /// 项目阶段 / 测量标准ID
        /// </summary>
        public Guid ProjectStandardId { get; internal set; }

        /// <summary>
        /// 使用的标准
        /// </summary>
        public string UseStandards { get; set; }

        /// <summary>
        /// 交付标准
        /// </summary>
        public string DeliveryStandard { get; internal set; }

        /// <summary>
        /// 项目阶段
        /// </summary>
        public string ProjectStage { get; set; }

        /// <summary>
        /// 任务类型
        /// </summary>
        public TaskTypeEnum TaskType { get; set; }

        /// <summary>
        /// 是否为新数据
        /// </summary>
        public bool IsNew { get; set; }

        /// <summary>
        /// 户数
        /// </summary>
        public string HouseHolds { get; set; }

        /// <summary>
        /// 任务名称
        /// </summary>
        public string TaskName { get; set; }

        /// <summary>
        /// 任务名称编码
        /// </summary>
        public int? TaskNameNo { get; set; }

        /// <summary>
        /// 测量项标准副本
        /// </summary>
        public string UsestandardAllInfo { get; set; }

        /// <summary>
        /// 交付标准：精装/毛坯
        /// </summary>
        public string SupportStandard { get; set; }

        /// <summary>
        /// 任务属性
        /// </summary>
        public string TaskProperty { get; set; }

        /// <summary>
        /// 户型图名称JSON数组
        /// </summary>
        public string HouseType { get; set; }

        /// <summary>
        /// 算法模板Id
        /// </summary>
        public Guid? AlgorithmParamTemplateGuid { get; set; }

        /// <summary>
        /// 算法模板名称
        /// </summary>
        public string AlgorithmParamTemplateName { get; set; }

        /// <summary>
        /// 是否移动端输入人工测量项
        /// </summary>
        public bool IsMobileInputManualMeasurement { get; set; } = false;

        public int? ManualMeasurementStatus { get; set; }

        /// <summary>
        /// 人工测量员Id
        /// </summary>
        public Guid? MobileInputUserGuid { get; set; }

        /// <summary>
        /// 人工测量员名称
        /// </summary>
        public string MobileInputUserName { get; set; }

        /// <summary>
        /// 点云文件数量
        /// </summary>
        public int DotCloudFileCount { get; set; }

        /// <summary>
        /// 同步状态
        /// </summary>
        public string SyncStatus { get; set; }

        /// <summary>
        /// 任务执行类型，‘1’：一键测量（采算），‘2’：只采
        /// </summary>
        public string TaskExecutionType { get; set; }

        /// <summary>
        /// 任务执行类型名称
        /// </summary>
        public string TaskExecutionTypeName { get; set; }

        public MeasureTypeEnum MeasureType { set; get; }

        public TemplateCraftEnum? TemplateCraft { set; get; }

        public List<string> RobotCategoryList { set; get; }

        public List<string> RobotCategoryIntList
        {
            get
            {
                List<string> list = new List<string>();
                RobotCategoryList?.ForEach(x =>
                {
                    if (!string.IsNullOrEmpty(x))
                        list.Add(Enum.Parse(typeof(RobotCategoryEnum), x).ToInt32().ToString());
                });
                return list;
            }
        }

        /// <summary>
        /// 内墙版本
        /// </summary>
        public string InnerWallVersion { set; get; }

        /// <summary>
        /// 二次结构类型：无：0，砌体：1，墙板：2，砌体和墙板：3
        /// </summary>
        public SecondaryStructureTypeEnum SecondaryStructureType { set; get; }
    }

    /// <summary>
    /// 子站点更新对象
    /// </summary>
    public class SubPointsUpdate
    {
        public Guid TaskID { get; set; }
        public Guid PointGuid { get; set; }
        public SubPointInfo[] SubPointArray { get; set; }

        /// <summary>
        /// -1 不更新，20为待合并，1未完成
        /// </summary>
        public int UpdateTaskStatus { get; set; }
    }

    /// <summary>
    /// Task表单对象
    /// </summary>
    public class TaskFm
    {
        /// <summary>
        /// 编辑任务时需要传递
        /// </summary>
        public Guid TaskId { get; set; }

        /// <summary>
        /// 项目Id
        /// </summary>
        [GuidRequired]
        public Guid ProjectId { get; set; }

        /// <summary>
        /// 楼栋
        /// </summary>
        [Required, MaxLength(32)]
        public string BuildingBlock { get; set; }

        /// <summary>
        /// 楼层
        /// </summary>
        [Required, MaxLength(32)]
        public string BuildingFloor { get; set; }

        /// <summary>
        /// 户数
        /// </summary>
        public string HouseHolds { get; set; }

        /// <summary>
        /// 任务类型
        /// </summary>
        [Required]
        public TaskTypeEnum TaskType { get; set; }

        /// <summary>
        /// 工艺
        /// </summary>
        [MaxLength(32)]
        public string Craft { get; set; }

        /// <summary>
        /// 项目阶段(标准库ID)
        /// </summary>
        [GuidRequired]
        public Guid ProjectStandardId { get; set; }

        /// <summary>
        /// 户型图
        /// </summary>
        [GuidRequired]
        public Guid HouseMapId { get; set; }

        /// <summary>
        /// 指定工作人员ID
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// 户型点信息
        /// </summary>
        public List<MeasurePoints> HouseMapPoints { get; set; }

        /// <summary>
        /// 使用的标准
        /// </summary>
        public UseStandard UseStandards { get; set; } = new UseStandard();

        /// <summary>
        /// 站点标准测量项
        /// </summary>
        public List<MsPointTypeDto> PointTypes { get; set; }

        /// <summary>
        /// 使用的标准
        /// </summary>
        public string UsestandardAllInfo { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [MaxLength(500)]
        public string Remarks { get; set; }

        /// <summary>
        /// 任务属性
        /// </summary>
        [MaxLength(10)]
        public string TaskProperty { get; set; }

        /// <summary>
        /// 户型JSON数组["1", "2", "3"]
        /// </summary>
        public string HouseType { get; set; }

        /// <summary>
        /// 算法模板Id
        /// </summary>
        public Guid AlgorithmParamTemplateGuid { get; set; }

        /// <summary>
        /// 算法模板名称
        /// </summary>
        public string AlgorithmParamTemplateName { get; set; }

        /// <summary>
        /// 是否移动端录入人工测量项
        /// </summary>
        public bool IsMobileInputManualMeasurement { get; set; }

        /// <summary>
        /// 人工测量员用户Id
        /// </summary>
        public Guid? MobileInputUserGuid { get; set; }

        /// <summary>
        /// 人工测量员名称
        /// </summary>
        public string MobileInputUserName { get; set; }

        /// <summary>
        /// 人工测量项状态
        /// </summary>
        public ManualMeasurementStatusEnum ManualMeasurementStatus { get; set; } = ManualMeasurementStatusEnum.未上传;

        /// <summary>
        /// 任务执行类型，‘1’：一键测量（采算），‘2’：只采;
        /// 前向兼容不设置为必填字段,默认未一键测量
        /// </summary>
        public string TaskExecutionType { get; set; } = "1";

        /// <summary>
        /// 测量任务类型：实测实量任务 = 0,机器人任务 = 1,建模任务 = 2, 排版任务 = 3；外立面任务 = 4
        /// </summary>
        [Required]
        public MeasureTypeEnum MeasureType { set; get; } = MeasureTypeEnum.实测实量任务;

        /// <summary>
        /// 任务模板工艺
        /// </summary>
        public TemplateCraftEnum? TemplateCraft { set; get; }

        /// <summary>
        /// 机器任务类型
        /// </summary>
        public List<RobotCategoryEnum> RobotCategory { set; get; }

        public string RobotCategoryStr { get; set; }

        /// <summary>
        /// 内墙版本
        /// </summary>
        public string InnerWallVersion { set; get; }

        /// <summary>
        /// 二次结构类型：无：0，砌体：1，墙板：2，砌体和墙板：3
        /// </summary>
        [Required]
        public SecondaryStructureTypeEnum SecondaryStructureType { set; get; } = SecondaryStructureTypeEnum.无;

        /// <summary>
        /// 测量应用：0平板端，1小程序
        /// </summary>
        [Required]
        public MeasureAPPEnum MeasureAPP { get; set; } = MeasureAPPEnum.小程序;

        /// <summary>
        /// 靠尺设置
        /// </summary>
        public RulerModeType RulerMode { get; set; } = RulerModeType.标准尺;

        /// <summary>
        /// 是否通过复制任务来创建新任务(非前端使用的参数)
        /// </summary>
        public bool IsCopyTask = false;
    }

    public class UseStandard
    {
        public List<string> man { get; set; } = new List<string>();
        public List<string> robot { get; set; } = new List<string>();
    }

    public class TaskSfm : TaskFm
    {
        public TaskSfm(MsTask obj)
        {
            obj.CopyTo(this);
        }

        public int? TaskNameNo { get; set; }

        /// <summary>
        /// 任务名称
        /// </summary>
        public string TaskName { get; set; }

        /// <summary>
        /// 任务类型
        /// </summary>
        public string TaskTypeStr => TaskType.ToString();

        /// <summary>
        /// 状态描述
        /// </summary>
        public string StatusStr { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public TaskStatus Status { get; set; }

        /// <summary>
        /// 项目名
        /// </summary>
        public string ProjectName { get; set; }

        /// <summary>
        /// 项目阶段
        /// </summary>
        public string ProjectStage { get; set; }

        /// <summary>
        /// 指定工作人员ID
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 创建人名字
        /// </summary>
        public string CreatedByName { get; set; }

        /// <summary>
        /// 使用的标准
        /// </summary>
        public List<StandardClass> UseStandardsStr { get; set; } = new List<StandardClass>();

        /// <summary>
        /// 支持的标准
        /// </summary>
        public string SupportStandard { get; set; }

        /// <summary>
        /// 是否为新建
        /// </summary>
        public bool IsNew { get; set; }

        /// <summary>
        /// 报告路径
        /// </summary>
        public string ReportPath { get; set; }

        /// <summary>
        /// 标准
        /// </summary>
        public string AllStandardInfo { get; set; }

        /// <summary>
        /// 测量点
        /// </summary>
        public List<TaskPointDto> Points { get; set; }

        /// <summary>
        /// 楼层信息
        /// </summary>
        public List<Detail> Details { get; set; }

        public string DetailsStr { get; set; }

        public string HouseHoldsStr
        {
            get
            {
                return GetHouseHoldsStr(this.HouseHolds, this.BuildingFloor);
            }
        }

        internal static string GetHouseHoldsStr(string houseHolds, string buildingFloor)
        {
            if (string.IsNullOrEmpty(houseHolds) || houseHolds == "-1")
            {
                return "";
            }
            else
            {
                int value1 = int.Parse(buildingFloor) * 100;
                int value2 = int.Parse(houseHolds);
                return (value1 + value2).ToString();
            }
        }

        /// <summary>
        /// 录入汇总数据否
        /// </summary>
        public bool HasMeasureItemSummaryData { get; set; }

        /// <summary>
        /// 录入人工测量项否
        /// </summary>
        public bool HasMeasureItemInputData { get; set; }

        public string UnqualifiedIndexList { get; set; }

        /// <summary>
        /// t点云已经删除,f点云未删除
        /// </summary>
        public bool DotCloudDeleted { get; set; }

        /// <summary>
        /// 机器人测量的站点标注文件路径
        /// </summary>
        public string RobotSitelabePath { get; set; }
    }

    /// <summary>
    /// 项目可选机器人
    /// </summary>
    public class ProjectRobot
    {
        /// <summary>
        /// 机器人ID
        /// </summary>
        public string RobotId { get; set; }

        /// <summary>
        /// 别名
        /// </summary>
        public string Alias { get; set; }
    }

    /// <summary>
    /// 项目可选成员
    /// </summary>
    public class ProjectMember
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public Guid UserId { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }
    }

    /// <summary>
    /// 项目户型图
    /// </summary>
    public class ProjectHouseMap
    {
        /// <summary>
        /// 户型图Id
        /// </summary>
        public Guid HouseMapId { get; set; }

        /// <summary>
        /// 户型图名字
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 户型图base64
        /// </summary>
        public string ImgSrc { get; set; }

        /// <summary>
        /// 项目名称
        /// </summary>
        public string ProjectName { get; set; }

        /// <summary>
        /// 项目ID
        /// </summary>
        public Guid ProjectId { get; set; }

        public DateTime CreatedDate { get; set; }

        public DateTime LastUpdatedDate { get; set; }

        /// <summary>
        /// 户型图类型
        /// </summary>
        public int MapType { get; set; }

        /// <summary>
        /// 户型图状态：0：启用，1：禁用
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// 户型图是否删除
        /// </summary>
        public bool IsDeleted { get; set; }

        /// <summary>
        /// 标准测量点
        /// </summary>
        public List<MeasurePoints> StandardMeasurePoints { get; set; }

        /// <summary>
        /// 测量面积
        /// </summary>
        public double MapArea { get; set; }

        /// <summary>
        /// 户型图楼栋楼层绑定
        /// </summary>
        public string[][] SelectBuildAndFloorValue { get; set; }

        /// <summary>
        /// 数据类型：0（实测户型图）、1（CAD数据）、2 （FMS模式数据），默认为0
        /// </summary>
        public int DataType { get; set; }

        /// <summary>
        /// 是否新增，否则是修改
        /// </summary>
        public bool IsNew { get; set; }
    }

    public class CopyHouseMapDto
    {
        /// <summary>
        /// 目标项目Id
        /// </summary>
        public Guid SourProjectId { get; set; }

        /// <summary>
        /// 目标项目Id
        /// </summary>
        public Guid DestProjectId { get; set; }

        /// <summary>
        /// 户型图Id
        /// </summary>
        public Guid HouseMapId { get; set; }
    }

    /// <summary>
    /// 设计值
    /// </summary>
    public class MeasureItem
    {
        /// <summary>
        /// 户型图Id
        /// </summary>
        public string HouseMapId { get; set; }

        /// <summary>
        /// 人工测量Id
        /// </summary>
        public Guid MSTaskPoint2ManualMeasurementId { get; set; }

        /// <summary>
        /// 站点Id
        /// </summary>
        public string PointId { get; set; }

        /// <summary>
        /// 测量项Id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 测量项代码号
        /// </summary>

        public string Code { get; set; }

        /// <summary>
        /// 测量项名称
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 设计值单位
        /// </summary>
        public string Unit { get; set; }

        public string Desc { get; set; }

        public string DisplayValue { get; set; }

        public string MeasuredDisplayValue { get; set; }

        /// <summary>
        /// 人工测量项：0，机器测量项：1
        /// </summary>
        public int Type { get; set; }

        public Guid ItemId { get; set; }

        /// <summary>
        /// 设计值
        /// </summary>
        public List<DesignValue> DesignValues { get; set; } = new List<DesignValue>();

        /// <summary>
        /// 实测值
        /// </summary>
        public List<MeasuredValue> MeasuredValues { get; set; } = new List<MeasuredValue>();
    }

    /// <summary>
    /// 设计值
    /// </summary>
    public class DesignValue
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public decimal Value { get; set; }
        public string Key { get; set; }

        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; }
    }

    /// <summary>
    /// 设计值
    /// </summary>
    public class DesignValueEx
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public decimal Value { get; set; }
        public string Key { get; set; }

        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; }
    }

    public class MeasuredValue
    {
        public string Title { get; set; }
        public string Key { get; set; }
        public decimal Value { get; set; }

        /// <summary>
        /// 单位
        /// </summary>
        public string Unit { get; set; }
    }

    /// <summary>
    /// 测量点
    /// </summary>
    public class MeasurePoints
    {
        /// <summary>
        /// 站点UUID
        /// </summary>
        public Guid PointId { get; set; }

        /// <summary>
        /// 次序
        /// </summary>
        public int pointIndex { get; set; }

        /// <summary>
        /// 测量点X坐标
        /// </summary>
        public string x { get; set; }

        /// <summary>
        /// 测量点Y坐标
        /// </summary>
        public string y { get; set; }

        /// <summary>
        /// X轴偏移值
        /// </summary>
        public string offsetX { get; set; }

        /// <summary>
        /// Y轴偏移值
        /// </summary>
        public string offsetY { get; set; }

        /// <summary>
        /// 站点类型
        /// </summary>
        public int Type { get; set; }

        /// <summary>
        /// 站点类型
        /// </summary>
        public Guid TypeId { get; set; }

        /// <summary>
        /// 站点类型名称
        /// </summary>
        public string TypeName { get; set; }

        /// <summary>
        /// 设计值
        /// </summary>
        public List<MeasureItem> MeasureItems { get; set; } = new List<MeasureItem>();

        /// <summary>
        /// 户型
        /// </summary>
        public string HouseTypeName { get; set; }

        public int? HouseTypeKey { get; set; }
    }

    public class MsTaskPointsEx : TaskPoints
    {
        public string MsPointTypeName { get; set; }
    }

    public class SyncTaskData : MsTask
    {
        /// <summary>
        /// 站点
        /// </summary>
        public List<MsTaskPointsEx> Points { get; set; }

        /// <summary>
        /// 测量项模板
        /// </summary>
        public List<MsTaskMeasureItemTemplate> MeasureItemTemplates { get; set; }

        /// <summary>
        /// 人工测量项
        /// </summary>
        public List<MSTaskPoint2ManualMeasurement> ManualMeasureItems { get; set; }

        /// <summary>
        /// 标准库
        /// </summary>
        public List<MsTaskStandard> Standards { get; set; }

        /// <summary>
        /// 汇总人工测量项
        /// </summary>
        public List<MsManualMeasureSummaryItems> ManualMeasureSummaryItems { get; set; }

        public string TaskTypeName { get; set; }

        public string ProjectStandardName { get; set; }
        public string ManualMeasurementInfo { get; set; }
    }
}